import UniRequest from '@/uni_modules/zg-request/js_sdk/UniRequest.js'
// import store from '@/store/index.js'
// import util from '@/common/js/util/index.js'
const http = new UniRequest({
	baseUrl: import.meta.env.VITE_PROXY_DOMAIN,
})
// 拦截器：发出请求前
http.interceptor.beforeRequest = (e:any) => {
	console.log(e)
	let header = {
		"x-token": '',
		"x-user-id": '',
	}
	// let token = util.getStorage('token') || store.getters.token;
	// let user = util.getStorage('user') || store.getters.user;
	// if(token){
	// 	header["x-token"] = token;
	// 	header["x-user-id"] = user.ID;
	// }else{
	// 	if (e.otherData.custom && e.otherData.custom.auth) {
	// 	  return Promise.reject(e)
	// 	}
	// }
	if(e.method == "GET"){
		let d = e.data.data;
		let _str = '';
		for(let o in d){
			if(d[o] == ''){
				delete(d[o])
			}
		}
	}
	e.header = {
		...header,
		// c: 3 // 演示拦截器header加参
	}
	return e
}
// 拦截器：返回请求前
http.interceptor.beforeResponse = (response: any) => {
	if (response.data.code == 200 || response.data.code == 0) {
	} else {
		if(response.data.code == 7){
			uni.showToast({
				icon: 'none',
				mask: true,
				title: response.data.msg,
				duration: 2000
			});
		}else if(response.data.code == 500){
			uni.showToast({
				icon: 'none',
				mask: true,
				title: response.data.msg,
				duration: 2000
			});
		}else if(response.data.code == 8848){
			// if(store.getters.token == ''){
			// 	return response.data;
			// }
			// uni.showToast({
			// 	icon: 'none',
			// 	title: 'token错误，刷新中',
			// 	duration: 2000,
			// });
			// store.dispatch('logOut');
			// store.dispatch('login');
		}else{
			if(!response.data.code){
				return response.data;
			}
			uni.showToast({
				icon: 'none',
				mask: true,
				title: '服务器开小差了',
				duration: 2000
			});
		}
		return response.data;
	}
	return response.data
}
// 拦截器：返回请求错误
http.interceptor.beforeResponseError = (errorCode: any) => {
	uni.showToast({
		icon: 'none',
		mask: true,
		title: '服务器开小差了',
		duration: 2000
	})
	return Promise.reject(errorCode)
}

export default http;
